package service

import (
	"context"
	pb "gitee.com/shuokeyun/med-bff/api/v1"
	"gitee.com/shuokeyun/med-bff/internal/domain"
)

func createOrderRequestConvDo(ctx context.Context, req *pb.CreateOrderRequest) *domain.CreateOrder {
	if req == nil {
		return nil
	}
	authUser := GetAuthUser(ctx)
	var ext *domain.OrderExt
	if req.Ext != nil {
		ext = &domain.OrderExt{
			Remark:            req.Ext.Remark,
			Department:        req.Ext.Department,
			OutpatientNo:      req.Ext.OutpatientNo,
			BedNo:             req.Ext.BedNo,
			ClinicalDiagnosis: req.Ext.ClinicalDiagnosis,
		}
	}
	barcodes := make([]*domain.OrderBarcodeCreate, len(req.Barcodes))
	for k, barcode := range req.Barcodes {
		barcodes[k] = &domain.OrderBarcodeCreate{
			Barcode:      barcode.Barcode,
			SamplingTime: barcode.SamplingTime,
			Consumable:   barcode.Consumable,
			Number:       barcode.Number,
			ItemCodes:    barcode.ItemCodes,
		}
	}
	users := make([]*domain.OrderUserCreate, len(req.Users))
	for k, user := range req.Users {
		users[k] = &domain.OrderUserCreate{
			UserId:   user.UserId,
			Name:     user.Name,
			Age:      user.Age,
			Sex:      user.Sex,
			Mobile:   user.Mobile,
			CardType: user.CardType,
			CardId:   user.CardId,
		}
	}
	return &domain.CreateOrder{
		AppId:             authUser.AppId,
		Brand:             req.Brand,
		MerchantId:        authUser.MerchantId,
		MerchantUserId:    authUser.MerchantUserId,
		Ext:               ext,
		Barcodes:          barcodes,
		OriginalItemCodes: req.OriginalItemCodes,
		Users:             users,
	}
}
